<template>
  <div class="authMana-container common-container">
    <div class="common-top">
      <span class="left title">权限管理</span>
      <el-button type="primary" size="small" @click="doModify('add')"
        v-if="hasPerm('system:role:edit')">添加权限组</el-button
      >
    </div>
    <div class="common-bot" v-if="hasPerm('system:role:list')">
      <el-table :data="tableData" style="width: 100%">
<!--        <el-table-column prop="id" label="ID">-->
<!--          <template slot-scope="scope">-->
<!--            <Briefly :value="''+scope.row.id" :length="10"/>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column
          prop="name"
          label="权限名"
          width="200"
        ></el-table-column>
        <el-table-column prop="detail" label="权限简述"></el-table-column>
        <el-table-column prop="queue" label="排序值" width="100"></el-table-column>
        <el-table-column
          label="是否启用" width="100"
          v-if="hasPerm('system:role:enable')"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enable"
              @change="doModify('enable', scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template
            slot-scope="scope"
            v-if="hasPerm('system:role:edit', 'system:role:del')"
          >
            <span
              class="span-modify edit"
              v-if="hasPerm('system:role:edit') && isMyData(scope.row.createBy)"
              @click.stop="doModify('edit', scope.row)"
              >编辑</span
            >
            <span
              class="span-modify del"
              v-if="hasPerm('system:role:del') && isMyData(scope.row.createBy)"
              @click.stop="doModify('del', scope.row)"
              >删除</span
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          v-if="showPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="nowPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="totalElement"
        ></el-pagination>
      </div>
    </div>
    <Modify
      @close="close"
      :id="modifyId"
      :type="modifyType"
      :showModify="showModify"
    />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Api from "@/api/index";
import Modify from "./modify.vue";
import Briefly from "@/layout/components/Briefly";

export default {
  components: { Modify, Briefly },
  computed: {
    ...mapGetters(["name"]),
  },
  data() {
    return {
      modifyType: "add",
      modifyId: "",
      showModify: false,
      showPage: false,
      tableData: [],
      limit: 10,
      nowPage: 1,
      totalElement: 0,
    };
  },
  watch: {
    nowPage(val) {
      this.getData();
      this.setStateData(val);
    },
    limit(val) {
      this.getData();
    },
  },
  methods: {
    close() {
      this.showModify = false;
      this.getData();
    },
    //
    setStateData(nowPage) {
      let stateData = {
        nowPage,
      };
      this.$router.replace({
        path: this.$route.path,
        query: {
          stateData: this.$base64.encode(JSON.stringify(stateData)),
        },
      });
    },
    handleSizeChange(val) {
      this.limit = val;
    },
    handleCurrentChange(val) {
      this.nowPage = val;
    },
    doModify(type, item = null) {
      switch (type) {
        case "add":
          this.modifyId = "";
          this.modifyType = type;
          this.showModify = true;
          break;
        case "edit":
          this.modifyId = item.id;
          this.modifyType = type;
          this.showModify = true;
          break;
        case "enable":
          Api.roleGroup
            .enable({
              targetId: item.id,
              yes: item.enable ? true : false,
            })
            .then((res) => {
              if (res.code == 200) {
                this.$message.success("操作成功");
                this.getData();
              }
            })
            .catch((e) => this.getData());
          break;
        case "del":
          this.$confirm("确认此操作?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            Api.roleGroup
              .del({
                targetIds: [item.id],
              })
              .then((res) => {
                if (res.code == 200) {
                  this.$message.success("操作成功");
                  this.getData();
                }
              })
              .catch((e) => this.getData());
          });
          break;
      }
    },
    getData() {
      Api.roleGroup
        .list({
          limit: this.limit,
          page: this.nowPage - 1,
        })
        .then((res) => {
          this.tableData = res.data;
          this.totalElement = res.total;
          this.showPage = true;
        });
    },
  },
  mounted() {
    if (this.$route.query.stateData) {
      console.log(JSON.parse(this.$base64.decode(this.$route.query.stateData)));
      const stateData = JSON.parse(
        this.$base64.decode(this.$route.query.stateData)
      );
      this.nowPage = stateData.nowPage;
      this.getData();
    } else {
      this.getData();
    }
  },
};
</script>

<style lang="scss" scoped></style>
